﻿namespace TriangleGames.TrainResEditor
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using UnityEngine;

    public static class DocHelpers
    {
        private const string MinicapPath = @"C:\Program Files\MiniCap\MiniCap.exe";
        private static StreamWriter sw;

        private static void CaptureRegion(Rect region, string savePath, string filename)
        {
            string str = " -save \"" + savePath + "\"";
            string str2 = string.Concat(new object[] { " -captureregion ", region.xMin, " ", region.yMin, " ", region.xMax, " ", region.yMax, " " });
            MiniCap(str2 + str + "\"" + filename + ".png\" -exit", true);
        }

        public static void CaptureStateInspectorAction(Rect region, string actionName, int actionIndex)
        {
            if (sw == null)
            {
                UnityEngine.Debug.LogError("必须首先调用StartStateActionListCapture!");
            }
            else
            {
                UnityEngine.Debug.Log("捕捉状态动作: " + actionName);
                string topic = FsmEditorUtility.StripNamespace(actionName);
                actionName = string.Concat(new object[] { FsmEditor.SelectedFsmGameObject.name, "_", FsmEditor.SelectedFsm.Name, "_", FsmEditor.SelectedState.Name, "_", actionIndex, "_", topic });
                region.x += FsmEditor.Window.position.x + FsmEditor.Inspector.View.x;
                region.y += (FsmEditor.Window.position.y + FsmEditor.Inspector.View.y) + 43f;
                CaptureRegion(region, @"C:\ActionScreens\SampleScreens\", actionName);
                sw.WriteLine("<tr>");
                string str2 = "https://www.triangle-games.com/default.asp?";
                int wikiPageNumber = EditorCommands.GetWikiPageNumber(topic);
                if (wikiPageNumber > 0)
                {
                    str2 = str2 + "W" + wikiPageNumber;
                }
                else
                {
                    str2 = str2 + "ixWiki=1&pg=pgSearchWiki&qWiki=" + topic;
                }
                sw.WriteLine("<td width=\"301px\"><a href = \"" + str2 + "\">");
                sw.WriteLine("<div id=\"actionSample\"><img src=\"http://www.triangle-games.com/docs/img/" + actionName + ".png\" title=\"\" /></div>");
                sw.WriteLine("</a></td>");
                sw.WriteLine("<td><p><strong>" + FsmEditorUtility.NicifyVariableName(topic) + "</strong></p>");
                sw.WriteLine("<p>TODO</p></td>");
                sw.WriteLine("</tr>");
            }
        }

        public static void EndStateActionListCapture()
        {
            if (sw != null)
            {
                sw.WriteLine("</table>");
                sw.WriteLine("</div>");
                sw.Close();
            }
        }

        private static void MiniCap(string arguments, bool waitForExit)
        {
            ProcessStartInfo info2 = new ProcessStartInfo();
            info2.Arguments = arguments;
            info2.FileName = @"C:\Program Files\MiniCap\MiniCap.exe";
            info2.WindowStyle = ProcessWindowStyle.Hidden;
            info2.CreateNoWindow = true;
            ProcessStartInfo startInfo = info2;
            if (waitForExit)
            {
                using (Process process = Process.Start(startInfo))
                {
                    process.WaitForExit();
                    return;
                }
            }
            Process.Start(startInfo);
        }

        public static void StartStateActionListCapture()
        {
            sw = File.CreateText(@"C:\ActionScreens\SampleScreens\" + (FsmEditor.SelectedFsmGameObject.name + "_" + FsmEditor.SelectedFsm.Name + "_" + FsmEditor.SelectedState.Name + ".txt"));
            sw.WriteLine("<div id=\"actionBreakdown\">");
            sw.WriteLine("<h3>Overview</h3>");
            sw.WriteLine("<p>TODO</p>");
            sw.WriteLine("<h3>Actions</h3>");
            sw.WriteLine("<table>");
        }
    }
}

